package com.polaris.live.adapter

import android.view.View
import android.widget.ImageView
import androidx.lifecycle.LifecycleCoroutineScope
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.resp.back_resp.RoomBannerItemResp
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.normal
import com.polaris.live.utils.playAsync
import com.polaris.live.utils.route.RouterUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import org.libpag.PAGImageView

/**
 * RoomBannerAbsAdapter
 *
 * @author Created by 半仙 on 2023/11/9/009 16:40
 */
class RoomBannerAbsAdapter(
    private val lifecycleScope: LifecycleCoroutineScope?
) : BaseBannerAdapter<RoomBannerItemResp>() {

    override fun bindData(
        holder: BaseViewHolder<RoomBannerItemResp>,
        data: RoomBannerItemResp?,
        position: Int,
        pageSize: Int,
    ) {
        val imageView = holder.findViewById<ImageView>(R.id.item_banner)
        val pagImageView = holder.findViewById<PAGImageView>(R.id.item_banner_pag)
        val redView = holder.findViewById<View>(R.id.room_item_red)
        val itemView = holder.findViewById<View>(R.id.item_view)
        data?.let {
            if (it.dot) {
                redView.visibility = View.VISIBLE
            } else {
                redView.visibility = View.GONE
            }
            if (it.pic.endsWith(PagConst.PAG)) {
                imageView.visibility = View.GONE
                pagImageView.visibility = View.VISIBLE
                lifecycleScope?.let { scope ->
                    pagImageView.playAsync(scope, it.pic)
                }
            } else {
                Glide.with(imageView.context).load(ImageUtils.imgPath(data.pic)).normal()
                    .into(holder.findViewById(R.id.item_banner))
                imageView.visibility = View.VISIBLE
                pagImageView.visibility = View.GONE
            }
            itemView.setOnSingleClickListener { view ->
                it.link?.let {
                    redView.visibility = View.GONE
                    RouterUtils.routerGoto(it)
                }
            }
        }
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.adapter_room_banner_abs
    }

}